const MAX = 9999999999;
import { checkStr } from "./validate";
const rules = {
  // 金额
  price: [
    {
      validator(rule, value, callback) {
        if (value < 0) {
          callback(new Error("价格不能小于0"));
        }
        if (!checkStr(value, "money")) {
          callback(new Error("价格格式错误"));
        }
        if (value > MAX) {
          callback(new Error("价格超过限制"));
        }
        callback();
      },
      trigger: "blur"
    }
  ],
  // 数量不包括0
  quantity: [
    {
      validator(rule, value, callback) {
        if (!checkStr(value, "positiveInteger")) {
          callback(new Error("请输入正整数"));
        }
        if (value < 1) {
          callback(new Error("必须大于0"));
        } else if (value > MAX) {
          callback(new Error(`不能大于${MAX}`));
        } else {
          callback();
        }
      },
      trigger: "blur"
    }
  ],
  // 数量
  quantitys: [
    {
      validator(rule, value, callback) {
        if (!checkStr(value, "positiveInteger")) {
          callback(new Error("请输入正整数"));
        }
        if (value > MAX) {
          callback(new Error(`不能大于${MAX}`));
        } else {
          callback();
        }
      },
      trigger: "blur"
    }
  ],
  // 电话
  phone: [
    { required: true, message: "手机号不能为空", trigger: "blur" },
    {
      validator(rule, value, callback) {
        if (!checkStr(value, "phone")) {
          callback(new Error("手机号格式不正确"));
        } else {
          callback();
        }
      },
      trigger: "blur"
    }
  ],
  // 电话或者座机
  tel: [
    { required: true, message: "请输入手机或座机号码", trigger: "blur" },
    {
      validator(rule, value, callback) {
        if (checkStr(value, "phone") || checkStr(value, "tel")) {
          callback();
        } else {
          callback(new Error("号码格式不正确"));
        }
      },
      trigger: "blur"
    }
  ],
  // 姓名
  name: [
    { required: true, message: "姓名不能为空", trigger: "blur" },
    {
      validator(rule, value, callback) {
        if (!checkStr(value, "chinese")) {
          callback(new Error("只能输入中文"));
        }
        if (value.length > 5) {
          callback(new Error("最多5个字"));
        } else {
          callback();
        }
      },
      trigger: "blur"
    }
  ],
  // 密码
  pwd: [
    {
      validator(rule, value, callback) {
        if (value && !checkStr(value, "pwd")) {
          callback(new Error("请输入8-12位字母或数字"));
        } else {
          callback();
        }
      },
      trigger: "blur"
    }
  ],
  // 身份证
  idCard: [
    {
      validator(rule, value, callback) {
        if (value && !checkStr(value, "card")) {
          callback(new Error("身份证格式错误"));
        } else {
          callback();
        }
      },
      trigger: "blur"
    }
  ]
};

export function createRules(arr) {
  const ret = {};
  arr.forEach(k => {
    ret[k] = rules[k];
  });
  return ret;
}
